package com.app.serve.api.controller;

import com.app.serve.api.entity.User;
import com.app.serve.api.service.UserService;
import com.app.serve.common.JsonResp;
import com.app.serve.enums.ErrorCodeEnum;
import com.app.serve.exception.JsonRespException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("/user")
    public User findUser() {
        User user1 = userService.findById();
        return user1;
    }

    @PostMapping("/register")
    public JsonResp register(@RequestParam("username") String username,
                             @RequestParam("password") String password) throws JsonRespException {

        if(StringUtils.isEmpty(username)) {
            return JsonResp.error(ErrorCodeEnum.NEED_USERNAME);
        }

        if(StringUtils.isEmpty(password)) {
            return JsonResp.error(ErrorCodeEnum.NEED_PASSWORD);
        }

        if(password.length() < 8) {
            return JsonResp.error(ErrorCodeEnum.PASSWORD_TOO_SHORT);
        }

        userService.register(username, password);
        return JsonResp.success("用户注册成功");

    }
}
